package codebehind
{
	import com.as3undohistory.manage.IUndoHistoryManager;
	import com.as3undohistory.manage.UndoHistoryManager;
	import com.as3undohistory.register.ObjectRegistration;
	
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.controls.CheckBox;
	import mx.controls.ColorPicker;
	import mx.controls.HSlider;
	import mx.core.Application;
	
	import shapes.CircleShape;

	public class ExampleApplication extends Application
	{
		private var _undoHistoryManager:IUndoHistoryManager;
		
		[Bindable]
		public var shapeCanvas:Canvas;
		[Bindable]
		public var circle:CircleShape;
		[Bindable]
		public var sldrAlpha:HSlider;
		[Bindable]
		public var cbUseTransaction:CheckBox;
		[Bindable]
		public var cpColor:ColorPicker;
		
		public function ExampleApplication()
		{
			super();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			circle = new CircleShape();
			circle.width = 50;
			circle.height = 50;
			circle.color = 0xff0000;
			circle.x = 10;
			circle.y = 10;
			shapeCanvas.addChild(circle);
		}
		
		protected function undo():void
		{
			_undoHistoryManager.undo();
			sldrAlpha.value = circle.alpha;
			cpColor.selectedColor = circle.color;
		}
		
		protected function onCreationComplete():void
		{
			_undoHistoryManager = new UndoHistoryManager(new ObjectRegistration());
			_undoHistoryManager.addObject(circle);
		}
		
		protected function setAlpaha():void
		{
			circle.alpha = sldrAlpha.value;
		}
		
		protected function startTrans(event:Event):void
		{
			if (cbUseTransaction.selected)
			{
				_undoHistoryManager.startTransaction();
			}
		}

		protected function endTrans(event:Event):void
		{
			if (cbUseTransaction.selected)
			{
				_undoHistoryManager.endTransaction();
			}
		}
		
		protected function setColor():void
		{
			circle.color = cpColor.selectedColor;
		}

	}
}